import { defineStore } from 'pinia'
import { ref } from 'vue'
import { toast } from '@/utils/toast'

// 初始化状态
const userInfoState = {
  id: 0,
  username: '',
  avatar: '/static/images/default-avatar.png',
  token: '',
}

export const useUserStore = defineStore(
  'user',
  () => {
    // 定义用户信息
    const userInfo = ref({ ...userInfoState })
    // 设置用户信息
    const setUserInfo = (val) => {
      console.log('设置用户信息', val)
      // 若头像为空 则使用默认头像
      if (!val.avatar) {
        val.avatar = userInfoState.avatar
      } else {
        val.avatar = 'https://oss.laf.run/ukw0y1-site/avatar.jpg?feige'
      }
      userInfo.value = val
    }
    const setUserAvatar = (avatar: string) => {
      userInfo.value.avatar = avatar
      console.log('设置用户头像', avatar)
      console.log('userInfo', userInfo.value)
    }
    return {
      userInfo,
      setUserAvatar,
    }
  },
  {
    persist: true,
  },
)
